//
//  SoruceCell.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 16/4/26.
//  Copyright © 2016年 sgone. All rights reserved.
//

#import "SoruceCell.h"
#import "FDLineView.h"
#import "Common.h"
#import "UIView+Helper.h"
#import "labelAdaptive.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

@interface SoruceCell () {
    
    
}

/**
 商品图片
 */
@property (nonatomic, strong)UIImageView *goodsImageView;
/**
 商品名称
 */
@property (nonatomic, strong)UILabel *nameLabel;
/**
 起订量
 */
@property (nonatomic, strong)UILabel *numberLabel;
/**
 运费
 */
@property (nonatomic, strong)UILabel *transLabel;
/**
 商品单价
 */
@property (nonatomic, strong)UILabel *priceLabel;
/**
 商品单位
 */
@property (nonatomic, strong)UILabel *unitLabel;
/**
 询价按钮
 */
@property (nonatomic, strong)UIButton *inquiryBtn;
/**
 下划线
 */
@property (nonatomic, strong)FDLineView *lineView;

@end

@implementation SoruceCell


- (UIImageView *)goodsImageView {
    
    if (_goodsImageView == nil) {
        
        _goodsImageView = [[UIImageView alloc] init];
        _goodsImageView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_goodsImageView];
        _goodsImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _goodsImageView;
}



- (UILabel *)nameLabel {
    
    if (_nameLabel == nil) {
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = FDFont_COLOR;
        _nameLabel.font = [UIFont systemFontOfSize:15 / FDLaOt];
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}



- (UILabel *)numberLabel {
    
    if (_numberLabel == nil) {
        
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.textColor = FDGFont_COLOR;
        _numberLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        [self addSubview:_numberLabel];
    }
    return _numberLabel;
}


- (UILabel *)transLabel {
    
    if (_transLabel == nil) {
        
        _transLabel = [[UILabel alloc] init];
        _transLabel.textColor = FDGFont_COLOR;
        _transLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        [self addSubview:_transLabel];
    }
    return _transLabel;
}


- (UILabel *)priceLabel {
    
    if (_priceLabel == nil) {
        
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = FDMainColorAla;
        _priceLabel.font = [UIFont systemFontOfSize:15 / FDLaOt];
        [self addSubview:_priceLabel];
    }
    return _priceLabel;
}



- (UILabel *)unitLabel {
    
    if (_unitLabel == nil) {
        
        _unitLabel = [[UILabel alloc] init];
        _unitLabel.textColor = FDGFont_COLOR;
        _unitLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        [self addSubview:_unitLabel];
    }
    return _unitLabel;
}


- (UIButton *)inquiryBtn {
    
    if (_inquiryBtn == nil) {
        
        _inquiryBtn = [[UIButton alloc] init];
        [_inquiryBtn setTitleColor:FDMainColorAla forState:UIControlStateNormal];
        _inquiryBtn.layer.borderColor = [FDMainColorAla CGColor];
        _inquiryBtn.layer.borderWidth = 0.5f;
        _inquiryBtn.layer.cornerRadius = 5.0f;
        _inquiryBtn.titleLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        [_inquiryBtn setTitle:@"点击询价" forState:UIControlStateNormal];
        [_inquiryBtn addTarget:self action:@selector(inquiryClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_inquiryBtn];
    }
    return _inquiryBtn;
}


- (FDLineView *)lineView {
    
    if (_lineView == nil) {
        
        _lineView = [[FDLineView alloc] init];
        [self addSubview:_lineView];
    }
    return _lineView;
}


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    
    static NSString *ID = @"SoruceCell";
    SoruceCell *cell  = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SoruceCell" owner:nil options:nil] objectAtIndex:0];
    }
    
    return cell;
    
}


- (void)awakeFromNib {
    
    [super awakeFromNib];

    
}



- (void)setMessageDic:(NSDictionary *)messageDic {
    
    _messageDic = messageDic;
    

    if ([messageDic[@"images"]  isKindOfClass:[NSNull class]] || [messageDic[@"images"] length] == 0 || ![messageDic isKindOfClass:[NSDictionary class]] )
    {

        self.goodsImageView.image = [UIImage imageNamed:@"backImage"];
    }
    else
    {
    
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",messageDic[@"images"]]] placeholderImage:[UIImage imageNamed:@"backImage"]];
 
    }
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f元",[messageDic[@"price"] floatValue]];
    
    NSString *priceUnit = @"";
    
    switch ([messageDic[@"price_unit"] integerValue]) {
        case 1:
            priceUnit = @"斤";
            break;
        case 2:
            priceUnit = @"公斤";
            break;
        case 3:
            priceUnit = @"箱";
            break;
            
        default:
            break;
    }
    self.unitLabel.text = [NSString stringWithFormat:@"/%@",priceUnit];
    
    if ([messageDic[@"shipment"] intValue] == 0 || [checkNull(messageDic[@"shipment"]) length] == 0) {
        
//        self.transLabel.text = @"面议";
        self.transLabel.text = [NSString stringWithFormat:@"运    费：面议"];

    } else
        self.transLabel.text = [NSString stringWithFormat:@"运    费：%.2f元",[messageDic[@"shipment"] floatValue]];
    
    NSString *startOrderUnit = nil;
    
    switch ([messageDic[@"least_unit"] integerValue]) {
        case 1:
            startOrderUnit = @"斤";
            break;
        case 2:
            startOrderUnit = @"公斤";
            break;
        case 3:
            startOrderUnit = @"箱";
            break;
            
        default:
            break;
    }
    self.numberLabel.text = [NSString stringWithFormat:@"起订量：%.2f%@",[messageDic[@"least_number"] floatValue],startOrderUnit];
    self.nameLabel.text = messageDic[@"title"];
    
}




- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat X = 8 / FDLaOt;
    CGFloat Y = 8 / FDLaOt;
    CGFloat Width = self.height - Y * 2;
    CGFloat Height = Width;
    
    self.goodsImageView.frame = CGRectMake(X, Y, Width, Height);
    
    self.nameLabel.frame = CGRectMake(self.goodsImageView.x + self.goodsImageView.width + X * 2, self.goodsImageView.y, ViewWidth - X * 4 - self.goodsImageView.width, 20 / FDLaOt);
    
    self.numberLabel.frame = CGRectMake(self.nameLabel.x, self.nameLabel.y + self.nameLabel.height + Y, self.nameLabel.width, 17 / FDLaOt);
    
    self.inquiryBtn.frame = CGRectMake(ViewWidth - X - 70 / FDLaOt, self.height - 25 / FDLaOt - Y, 70 / FDLaOt, 25 / FDLaOt);
    
    self.transLabel.frame = CGRectMake(self.nameLabel.x, self.numberLabel.y + self.numberLabel.height + Y, self.numberLabel.width - self.inquiryBtn.width, self.numberLabel.height);
    
    CGSize priceSize = [labelAdaptive sizeWithString:self.priceLabel.text font:self.priceLabel.font];
    CGSize unitSize = [labelAdaptive sizeWithString:self.unitLabel.text font:self.unitLabel.font];
    
    self.priceLabel.frame = CGRectMake(self.transLabel.x, self.transLabel.y + self.transLabel.height + Y, priceSize.width, 20 / FDLaOt);
    
    self.unitLabel.frame = CGRectMake(self.priceLabel.x + self.priceLabel.width + 2, self.priceLabel.y, unitSize.width, 20 / FDLaOt);
    
    self.lineView.frame = CGRectMake(0, self.height - 0.5, ViewWidth, 0.5);
}




- (void)inquiryClick {
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"inquiryClick" object:nil userInfo:self.messageDic];
}




@end
